package com.shr.newcoder;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class NtesGame_3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int column = sc.nextInt();
		
		char[][] pathMatrix = new char[row][column] ;
		Status startStatus = null ;
		Status boxStatus = null;
		Status endStatus = null;
		for(int i = 0;i<pathMatrix.length;i++){
			String str = sc.next();
			for(int j = 0;j<pathMatrix[i].length;j++){
				char ch = str.charAt(j) ;
				pathMatrix[i][j] = ch;
				if(ch == 'X')
					startStatus = new Status(i, j);
				else if(ch == '*')
					boxStatus = new Status(i,j) ;
				else if(ch == '@'){
					endStatus = new Status(i, j);
				}
					
			}
		}
		sc.close();
		Queue<Status> queue = new LinkedList<>() ;
		startStatus.boxStatus = boxStatus ;
		queue.add(startStatus);
		List<Status> searchedList = new ArrayList<>();
		while(!queue.isEmpty()){
			Status status = queue.poll() ;
			searchedList.add(status);
			if(endStatus.equalsOnCoord(status.boxStatus)){
				System.out.println(status.toStartDistance);
				return;
			}
			else{
				List<Status> childStatus = getChildStstus(status, pathMatrix);
				for(Status aStatus : childStatus){
					if(!searchedList.contains(aStatus)){
						queue.add(aStatus) ;
					}
				}
				
			}
		}
		System.out.println(-1);
		
	}


	private static List<Status> getChildStstus(Status status, char[][] pathMatrix) {
		int xCoord = status.x;
		int yCoord = status.y;
		int toStart = status.toStartDistance;
		Status boxStatus = status.boxStatus;
		List<Status> cStatusList = new ArrayList<>() ;
		if(xCoord - 1 >= 0 && pathMatrix[xCoord - 1][yCoord] != '#'){
			if(xCoord - 1 == boxStatus.x && yCoord == boxStatus.y){
				if(boxStatus.x-1 >= 0 && pathMatrix[boxStatus.x-1][yCoord] != '#'){
					Status newBoxStatus = new Status(boxStatus.x - 1, boxStatus.y);
					Status cStatus = new Status(xCoord-1, yCoord,toStart + 1,newBoxStatus) ;
					cStatusList.add(cStatus);
				}
			}else{
				Status cStatus = new Status(xCoord-1, yCoord,toStart + 1, boxStatus) ;
				cStatusList.add(cStatus) ;
			}
		}
		
		if(yCoord - 1 >= 0 && pathMatrix[xCoord][yCoord-1] != '#'){
			if(xCoord == boxStatus.x && yCoord -1 == boxStatus.y){
				if(boxStatus.y-1 >= 0 && pathMatrix[boxStatus.x][boxStatus.y-1] !='#'){
					Status newBoxStatus = new Status(boxStatus.x , boxStatus.y - 1);
					Status cStatus = new Status(xCoord, yCoord-1,toStart + 1,newBoxStatus) ;
					cStatusList.add(cStatus);
				}
			}else{
				Status cStatus = new Status(xCoord, yCoord-1,toStart + 1,boxStatus) ;
				cStatusList.add(cStatus);
			}
		}
		
		if(xCoord + 1 < pathMatrix.length && pathMatrix[xCoord+1][yCoord] != '#'){
			if(xCoord + 1 == boxStatus.x && yCoord == boxStatus.y){
				if(boxStatus.x+1 < pathMatrix.length && pathMatrix[boxStatus.x+1][boxStatus.y] !='#'){
					Status newBoxStatus = new Status(boxStatus.x+1 , boxStatus.y);
					Status cStatus = new Status(xCoord+1, yCoord,toStart + 1,newBoxStatus) ;
					cStatusList.add(cStatus);
				}
			}else{
				Status cStatus = new Status(xCoord+1, yCoord,toStart + 1,boxStatus) ;
				cStatusList.add(cStatus);
			}
		}
		if(yCoord + 1 < pathMatrix[xCoord].length && pathMatrix[xCoord][yCoord+1] != '#'){
			if(xCoord == boxStatus.x && yCoord + 1 == boxStatus.y){
				if(boxStatus.y+1 < pathMatrix[xCoord].length && pathMatrix[boxStatus.x][boxStatus.y+1] !='#'){
					Status newBoxStatus = new Status(boxStatus.x , boxStatus.y+1);
					Status cStatus = new Status(xCoord, yCoord+1,toStart + 1,newBoxStatus) ;
					cStatusList.add(cStatus);
				}
			}else{
				Status cStatus = new Status(xCoord, yCoord+1,toStart + 1,boxStatus) ;
				cStatusList.add(cStatus);
			}
		}
			
		return cStatusList;
	}
}


class Status{
	int x;
	int y;
	int toStartDistance;
	Status boxStatus ;
	
	Status(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public boolean equalsOnCoord(Status boxStatus2) {
		return this.x == boxStatus2.x && this.y == boxStatus2.y;
	}

	Status(int x,int y, int toStart, Status boxStatus){
		this.x = x;
		this.y = y;
		this.toStartDistance = toStart ;
		this.boxStatus = boxStatus ;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		Status statusObj = (Status) obj ;
		return this.x == statusObj.x && this.y == statusObj.y && this.boxStatus.equalsOnCoord(statusObj.boxStatus);
	}
}
